package com.imy.lua.helper.properties;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.imy.common.util.ALog;
import com.imy.common.util.FileUtils;
import com.imy.common.util.ToastUtil;
import com.imy.common.util.UiUtil;
import com.imy.lua.Constant;
import com.imy.lua.LuaCore;
import com.imy.lua.helper.opencv.ImageFindHelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class PropertiesLoaderHelper {
    private static volatile Properties properties;


    private static Properties loadProperties(Context context, String path) {
        String assets = "android_asset/";
        if (path.contains(assets)) {
            //替换
            int i = path.indexOf(assets);
            path = path.substring(i + assets.length());
            try {
                ALog.e("path:" + path);
                InputStream open = context.getAssets().open(path);
                Properties properties = new Properties();
                properties.load(open);
                return properties;
            } catch (IOException e) {
                ALog.e("android_asset load Properties error:" + e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
        Properties properties = new Properties();
        try {
            FileInputStream fileInputStream = new FileInputStream(path);
            InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8); //最后的"GBK"根据文件属性而定，如果不行，改成"UTF-8"试试
            properties.load(reader);
            ALog.e("loadProperties 11111111");
            return properties;
        } catch (Exception e) {
            ALog.e("loadProperties error:" + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public static Properties loadGlobal(Context context, String filePath) {
        properties = loadProperties(context, filePath);
        if (properties != null) {
            String project = properties.getProperty("project");
            String path = parserToAndroidPath(project);
            FileUtils.createFileDir(path);
            String res = properties.getProperty("res");
            FileUtils.createFileDir(parserToAndroidPath(res));
        }
        return properties;

    }

    public static boolean isSameMetrics() {
        int deviceWidthInPortrait = ImageFindHelper.get().getDeviceWidthInPortrait();
        int scriptWidth = getScriptWidth();
        return deviceWidthInPortrait == scriptWidth;
    }

    public static int getScriptWidth() {
        return Integer.parseInt(properties.getProperty("width"));
    }

    public static int getScriptHeight() {
        return Integer.parseInt(properties.getProperty("height"));
    }

    public static Properties getProperties() {
        return properties;
    }

    /**
     * 获取项目的工程资源路径
     *
     * @return
     */
    public static String getProjectRes() {
        String res = properties.getProperty("res");
        return parserToAndroidPath(res);
    }
    public static String parserToAndroidPath(String path) {
        if (path.contains("/sd/")) {
            File externalStorageDirectory = Environment.getExternalStorageDirectory();
            return path.replace("/sd/", externalStorageDirectory.getAbsolutePath() + "/");
        } else {
            return path;
        }
    }

    private static Properties loadAssetsProperties(Context context) {
        Properties properties = new Properties();
        try {
            InputStream open = context.getAssets().open("global.properties");
            properties.load(open);
            return properties;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static boolean createBaseGlobalConfig(String path, String fileName) {
        File file = new File(path);

        int deviceWidth = UiUtil.getDeviceWidth(LuaCore.get().getContext());
        int deviceHeight = UiUtil.getDeviceHeight(LuaCore.get().getContext());
        try {
            RandomAccessFile rwd = new RandomAccessFile(file, "rwd");

            String config="#使用什么模式\n" +
                    "mode=auto\n" +
                    "#工程路径\n" +
                    "project=/sd/LuaAndroid/"+fileName+"\n" +
                    "#资源路径;\n" +
                    "res=/sd/LuaAndroid/"+fileName+"/res\n" +
                    "#设置脚本开发的分辨率\n" +
                    "width="+deviceWidth+"\n" +
                    "height="+deviceHeight;
            rwd.seek(file.length());
            rwd.write(config.getBytes());
            rwd.close();
            return true;
        } catch (IOException e) {
            ALog.e("创建 通用全局配置失败:"+e.getMessage() +" path:"+path);
            e.printStackTrace();
            return false;
        }

    }
}
